<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="车位号" prop="carportName">
        <el-input
          v-model="queryParams.carportName"
          placeholder="请输入车位号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="业主信息" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入业主信息"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区域" prop="aName">
        <el-input
          v-model="queryParams.aName"
          placeholder="请输入区域"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="carportsellList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="sellId" />
      <el-table-column label="车位号" align="center" prop="carportName" />
      <el-table-column label="业主信息" align="center" prop="name" />
      <el-table-column label="区域" align="center" prop="aname" />
      <el-table-column label="销售时间" align="center" prop="selldate" />
      <el-table-column label="车位价格" align="center" prop="sellPrice" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改车位销售对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="与t_carport表主键关联，车位号" prop="cid">
          <el-input v-model="form.cid" placeholder="请输入与t_carport表主键关联，车位号" />
        </el-form-item>
        <el-form-item label="与t_proprietor表主键关联，业主信息" prop="pid">
          <el-input v-model="form.pid" placeholder="请输入与t_proprietor表主键关联，业主信息" />
        </el-form-item>
        <el-form-item label="与t_area表主键关联，区域Id" prop="typestate">
          <el-input v-model="form.typestate" placeholder="请输入与t_area表主键关联，区域Id" />
        </el-form-item>
        <el-form-item label="销售时间" prop="selldate">
          <el-input v-model="form.selldate" placeholder="请输入销售时间" />
        </el-form-item>
        <el-form-item label="车位价格" prop="sellprice">
          <el-input v-model="form.sellprice" placeholder="请输入车位价格" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCarportsell, getCarportsell, delCarportsell, addCarportsell, updateCarportsell } from "@/api/system/carportsell";
import {getCarportSellinfo} from "../../../api/system/carportsell";

export default {
  name: "Carportsell",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车位销售表格数据
      carportsellList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        carportName: null,
        name: null,
        aname: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        pid: [
          { required: true, message: "与t_proprietor表主键关联，业主信息不能为空", trigger: "blur" }
        ],
        typestate: [
          { required: true, message: "与t_area表主键关联，区域Id不能为空", trigger: "blur" }
        ],
        selldate: [
          { required: true, message: "销售时间不能为空", trigger: "blur" }
        ],
        sellprice: [
          { required: true, message: "车位价格不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询车位销售列表 */
    getList() {
      this.loading = true;
      getCarportSellinfo(this.queryParams).then(response => {
        this.carportsellList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        carportName: null,
        name: null,
        aName: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.sellid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加车位销售";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const sellid = row.sellid || this.ids
      getCarportsell(sellid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改车位销售";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.sellid != null) {
            updateCarportsell(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCarportsell(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const sellids = row.sellid || this.ids;
      this.$modal.confirm('是否确认删除车位销售编号为"' + sellids + '"的数据项？').then(function() {
        return delCarportsell(sellids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/carportsell/export', {
        ...this.queryParams
      }, `carportsell_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
